var exp = require('express')
var fs = require('fs')
var tem = require('art-template')
const cookieParser = require('cookie-parser')
var mutuer = require('multer')
const { User, address } = require('./lib/mongodb')
const bodyParser = require('body-parser')
var app = exp()
// app.use(bodyParser.urlencoded({extends:true}))
app.use(exp.static('www'))
app.use(bodyParser.urlencoded({ extended: true }))
app.engine('.html', require('express-art-template'))
app.set('view engine', 'html')
app.set('views', './view')
tem.defaults.extname = '.html'
app.use(cookieParser())
app.use(require('./router/page'))
app.use(require('./router/item'))
app.get('/resd', (req, res) => {
  User.findOne({
    $or: [
      { email: req.query.email },
      { phone1: req.query.phone1 }
    ]
  }, (err, data) => {
    if (data) {
      res.send('用户名已经存在')
    } else {
      User(req.query).save()
      res.send('注册成功')
    }
  })

})
app.get('/log', (req, res) => {
  User.findOne({
    $or: [
      { email: req.query.username },
      { phone1: req.query.username }
    ]
  }, (err, data) => {
    if (data) {
      if (data.psw == req.query.psw) {
        res.cookie('user', data.username)
        res.cookie('phone', data.id)
        res.send('登陆成功')
      } else {
        res.send('密码错误')
      }
    } else {
      res.send('该用户未注册')
    }
  })
})
app.get('/conadd', (req, res) => {
  req.query.phone1 = req.cookies.phone
  if (req.query.cons) {
    if (req.query.constel) {
      if (req.query.dele && req.query.email && req.query.city) {
        address(req.query).save()
        res.send('保存成功')
      } else {
        res.send('请输入地址邮编')
      }
    } else {
      res.send('请输入手机号')
    }
  } else {
    res.send('请输入用户名')
  }
})
app.get('/updata1', (req, res) => {
  req.query.phone1 = req.cookies.phone
  if (req.query.cons) {
    if (req.query.constel) {
      if (req.query.dele && req.query.email && req.query.city) {
        console.log(req.cookies.idup);
        address.updateOne({
          _id: req.cookies.idup
        }, req.query, (err, data) => {
          if (!err) {
            res.send('更新成功')
          }

        })

      } else {
        res.send('请输入地址邮编')
      }
    } else {
      res.send('请输入手机号')
    }
  } else {
    res.send('请输入用户名')
  }
})
app.get('/delel', (req, res) => {
  console.log(req.query.id);
  address.deleteOne({
    _id: req.query.id
  }, (err, data) => {
    res.send('删除成功')
  })
})
app.get('')
app.listen(80, () => {
  console.log('服务器正在运行...');
})